package com.demo.download.server.impl;

import com.demo.download.server.AbstractDisplayDownloadSpeedResponseExtractor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ByteArrayResponseExtractor extends AbstractDisplayDownloadSpeedResponseExtractor<byte[]> {

    private long byteCount;

    @Override
    protected byte[] doExtractData(ClientHttpResponse response) throws IOException {

        long contentLength = response.getHeaders().getContentLength();
        ByteArrayOutputStream out = new ByteArrayOutputStream(contentLength >= 0 ? (int)contentLength : StreamUtils.BUFFER_SIZE);
        InputStream in = response.getBody();
        int bytesRead;
        for(byte[] buffer = new byte[4096]; (bytesRead = in.read(buffer)) != -1; byteCount += bytesRead){
            out.write(buffer,0,bytesRead);
        }
        out.flush();
        return out.toByteArray();
    }

    @Override
    protected long getAlreadyDownloadLength() {
        return byteCount;
    }
}
